﻿using System.Web.Mvc;
using System.Linq;
using System.Web.Security;
using GameSchoolApplication.ViewModels;
using GameSchoolApplication.Models;
using GameSchoolApplication.Repository;
using System.Collections.Generic;
using GameSchoolApplication.Helper;


namespace GameSchoolApplication.Controllers
{
	[Authorize]
	public class HomeController : Controller
	{
		private GameSchoolWebContext _db = new GameSchoolWebContext();

		/// <summary>
		/// Sends the current user to his specified home-view
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public ActionResult Index()
		{
            Person person = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault();
            if (person.Group == "Student")
            {
                return RedirectToAction("StudentIndex");
            }
            if (person.Group == "Teacher")
            {
                return RedirectToAction("TeacherGames", "Game");
            }
            return RedirectToAction("AdministratorIndex");            
		}

		/// <summary>
		/// Gets all the information for the current student
		/// </summary>
		/// <returns></returns>
		[HttpGet]
        public ActionResult StudentIndex()
        {
        	var person = _db.Persons.Where(m => m.UserName == User.Identity.Name).SingleOrDefault();
            return View(person);
        }

		/// <summary>
		/// Returns to the hom page for the administrator
		/// </summary>
		/// <returns></returns>
		public ActionResult AdministratorIndex()
        {
            return View();
        }

		/// <summary>
		/// About us
		/// </summary>
		/// <returns></returns>
		public ActionResult About()
		{
			return View();
		}

		protected override void Dispose(bool disposing)
		{
			_db.Dispose();
			base.Dispose(disposing);
		}
	}
}
